using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Colorprj.Challan;
using Colorprj.Common;

namespace Colorprj
{
    public partial class frmrptChallan : Form
    {
        ChallanManager cm = new ChallanManager();
        public String challanNo = string.Empty;
        public frmrptChallan()
        {
            InitializeComponent();
        }

        private void frmrptChallan_Load(object sender, EventArgs e)
        {

            //this.reportViewer1.RefreshReport();
            if (!string.IsNullOrEmpty(challanNo))
            {
                showReport(challanNo);
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("please input challan No ");
                return;
            }
            challanNo = textBox1.Text;
            showReport(challanNo);
        }

        private void showReport(string challanNo)
        {

            ReportDocument cryRpt = new Report.rptChallan();
            string appPath = Path.GetDirectoryName(Application.ExecutablePath);
            //cryRpt.Load(Utility.GetRptLocation());
            TableLogOnInfo myLogonInfo = new TableLogOnInfo();


            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            ParameterValues crParameterValues1 = new ParameterValues();
            ParameterFieldDefinition crParameterFieldDefinition1;
            ParameterDiscreteValue crParameterDiscreteValue1 = new ParameterDiscreteValue();


            ParameterValues crParameterValuesAdd1 = new ParameterValues();
            ParameterFieldDefinition crParameterFieldDefinitionAdd1;
            ParameterDiscreteValue crParameterDiscreteValueAdd1 = new ParameterDiscreteValue();



            ParameterValues crParameterValuesAdd2 = new ParameterValues();
            ParameterFieldDefinition crParameterFieldDefinitionAdd2;
            ParameterDiscreteValue crParameterDiscreteValueAdd2 = new ParameterDiscreteValue();


            ParameterValues crParameterValuesEmail = new ParameterValues();
            ParameterFieldDefinition crParameterFieldDefinitionEmail;
            ParameterDiscreteValue crParameterDiscreteValueEmail = new ParameterDiscreteValue();

            ParameterValues crParameterValuesPhone = new ParameterValues();
            ParameterFieldDefinition crParameterFieldDefinitionPhone;
            ParameterDiscreteValue crParameterDiscreteValuePhone = new ParameterDiscreteValue();

            ParameterValues crParameterValuesTIN = new ParameterValues();
            ParameterFieldDefinition crParameterFieldDefinitionTIN;
            ParameterDiscreteValue crParameterDiscreteValueTIN = new ParameterDiscreteValue();



            //           432 clerk colony,Near vigayvargiya dharmshala,indore-2(MP)
            //Email krishnaagencies30@yahoo.com ph. 0731-3225344
           

       
            cryRpt.DataSourceConnections[0].SetConnection(Globalclass.DsnName, Globalclass.DbName, "atul", "vikas");

            cryRpt.SetDatabaseLogon("atul", "vikas", Globalclass.DsnName, Globalclass.DbName , false);

             
            crParameterDiscreteValue.Value = challanNo;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["ChallanNo"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterDiscreteValue1.Value = Globalclass.CompanyName; ;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition1 = crParameterFieldDefinitions["cName"];
            crParameterValues1 = crParameterFieldDefinition1.CurrentValues;


            crParameterDiscreteValueAdd1.Value = Globalclass.Address1 + " " + Globalclass.Address2; ;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinitionAdd1 = crParameterFieldDefinitions["cAdd1"];
            crParameterValuesAdd1 = crParameterFieldDefinition1.CurrentValues;

            crParameterDiscreteValueAdd2.Value = "";
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinitionAdd2 = crParameterFieldDefinitions["cAdd2"];
            crParameterValuesAdd2 = crParameterFieldDefinitionAdd2.CurrentValues;

            crParameterDiscreteValueEmail.Value = Globalclass.Email + " " + Globalclass.PhoneNo; 
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinitionEmail = crParameterFieldDefinitions["cEmail"];
            crParameterValuesEmail = crParameterFieldDefinitionEmail.CurrentValues;

            crParameterDiscreteValuePhone.Value = "";
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinitionPhone = crParameterFieldDefinitions["cPhone"];
            crParameterValuesPhone = crParameterFieldDefinitionPhone.CurrentValues;


            crParameterDiscreteValueTIN.Value = Globalclass.TinNo;
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinitionTIN = crParameterFieldDefinitions["cTIN"];
            crParameterValuesTIN= crParameterFieldDefinitionTIN.CurrentValues;

            

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);


            crParameterValues1.Add(crParameterDiscreteValue1);
            crParameterFieldDefinition1.ApplyCurrentValues(crParameterValues1);


            crParameterValuesAdd1.Add(crParameterDiscreteValueAdd1);
            crParameterFieldDefinitionAdd1.ApplyCurrentValues(crParameterValuesAdd1);

            crParameterValuesAdd2.Add(crParameterDiscreteValueAdd2);
            crParameterFieldDefinitionAdd2.ApplyCurrentValues(crParameterValuesAdd2);


            crParameterValuesEmail.Add(crParameterDiscreteValueEmail);
            crParameterFieldDefinitionEmail.ApplyCurrentValues(crParameterValuesEmail);

            crParameterValuesPhone.Add(crParameterDiscreteValuePhone);
            crParameterFieldDefinitionPhone.ApplyCurrentValues(crParameterValuesPhone);

            crParameterValuesTIN.Add(crParameterDiscreteValueTIN);
            crParameterFieldDefinitionTIN.ApplyCurrentValues(crParameterValuesTIN);

            crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh();

        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }



        private void btnPrint_Click(System.Object sender, System.EventArgs e)
        {
            PrintDialog printDialog1 = new PrintDialog();
            System.Drawing.Printing.PrintDocument printDocument1 = new System.Drawing.Printing.PrintDocument();
            //Open the PrintDialog
            printDialog1.Document = printDocument1;

            DialogResult dr = printDialog1.ShowDialog();

            //Here's where you can catch them aborting the print..

            if (dr == System.Windows.Forms.DialogResult.OK)
            {


                cm.UpdPrintFlg(challanNo);
                //Get the Copy times
                int nCopies = printDocument1.PrinterSettings.Copies;
                //Get the number of Start Page
                int sPage = printDocument1.PrinterSettings.FromPage;
                //Get the number of End page
                int ePage = printDocument1.PrinterSettings.ToPage;
                //Get the printer name
                string PrinterName = printDocument1.PrinterSettings.PrinterName;

                CrystalDecisions.CrystalReports.Engine.ReportDocument crReportDocument = default(CrystalDecisions.CrystalReports.Engine.ReportDocument);
                //Create an instance of a report
                crReportDocument = (ReportDocument)this.crystalReportViewer1.ReportSource;
                try
                {
                    crReportDocument.PrintOptions.PrinterName = PrinterName;
                    crReportDocument.PrintToPrinter(nCopies, false, sPage, ePage);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void crystalReportViewer1_Load(object sender, EventArgs e)
        {

        }

        private void crystalReportViewer1_Load_1(object sender, EventArgs e)
        {

        }

        //private void btnPrint_Click_1(object sender, EventArgs e)
        //{

        //}
    }
}